package com.meixiang.wxapi;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.meixiang.R;
import com.meixiang.activity.account.managers.myPurse.RechargeSucceedActivity;
import com.meixiang.activity.homes.service.OrderSubmissionSuccessfulActivity;
import com.meixiang.activity.homes.shopping.CashierDeskActivity;
import com.meixiang.global.GlobalType;
import com.meixiang.global.apiConfig;
import com.meixiang.util.AbStrUtil;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;


/**
 * 微信支付回调
 * */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	public   static   String msg = "";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		api=WXAPIFactory.createWXAPI(this, apiConfig.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		Log.d("TAG", "onPayFinish, errCode=" + resp.errCode);


		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			if (resp.errCode == 0) {
				msg = "支付成功";
			} else if (resp.errCode == -1) {
				msg = "已取消支付";
			} else if (resp.errCode == -2) {
				msg = "支付失败";
			}
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.app_tip);
			builder.setMessage(getString(R.string.pay_result_callback_msg, msg));
			builder.show();
			if(resp.errCode == 0){
				Intent intent=new Intent();
				if (CashierDeskActivity.payType.equals(CashierDeskActivity.ORDER_PAY)) {
					// 订单支付，跳转到订单支付成功页面
					intent .setClass(WXPayEntryActivity.this, OrderSubmissionSuccessfulActivity.class);
					if(!AbStrUtil.isEmpty(getIntent().getStringExtra(GlobalType.ORDER_PAYMENT_TYPE))
							&&CashierDeskActivity.MY_ORDER_SHOPPING_ORDER.equals(getIntent().getStringExtra(GlobalType.ORDER_PAYMENT_TYPE))){
						notifyOrderList();
					}
					intent.putExtra("payCode","WX");
					intent.putExtra(CashierDeskActivity.ORDER_TYPE,getIntent().getStringExtra(CashierDeskActivity.ORDER_TYPE));
				} else {
					// 钱包充值、会员卡充值，跳转到充值成功页面
					intent .setClass(WXPayEntryActivity.this, RechargeSucceedActivity.class);
				}
				intent.putExtra(CashierDeskActivity.ORDER_RESULT, CashierDeskActivity.mOrderResult);
				intent.putExtra(CashierDeskActivity.GOTO_ACTIVITY, CashierDeskActivity.gotoActivityMap);
				startActivity(intent);
			}


//

		}
	}

	private void notifyOrderList(){
		Intent intent=new Intent(GlobalType.ORDER_DETAIL_OPERATION);
//        intent.putExtra("orderId",orderId);
		sendBroadcast(intent);
		this.finish();
	}
}